<?php

	 function now()
	{
		return date("Y-m-d H:i:s");
	}

	function alert($msg,$url='')
	{
		if(empty($url)) $url=$_SERVER['HTTP_REFERER'];
		if($url==2){exit("<script>alert('".$msg."');window.history.go(-2);</script>");}
	    if(!isset($url)) $url="http://".$_SERVER['HTTP_HOST']."/";
		exit("<script>alert('".$msg."');window.location.href='".$url."'</script>");
	}

	 function getIp()

    { 

        if (@$_SERVER['HTTP_CLIENT_IP'] && $_SERVER['HTTP_CLIENT_IP']!='unknown') {   

            $ip = $_SERVER['HTTP_CLIENT_IP'];   

        } elseif (@$_SERVER['HTTP_X_FORWARDED_FOR'] && $_SERVER['HTTP_X_FORWARDED_FOR']!='unknown') {   

            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];   

        } else {   

            $ip = $_SERVER['REMOTE_ADDR'];   

        }   

        return $ip;   

    }


/*
	function temp($path,$file){
		
		$file_path=TEMP_PATH.$path."/".$file;
		$cache_path=CACHE_PATH.$path."/".$file.".tpl.php";
		
		if($path=="/"){
			$file_path=TEMP_PATH.$file;
			$cache_path=CACHE_PATH.$file.".tpl.php";
		}

		if(!file_exists($cache_path)||@filemtime($file_path) > @filemtime($cache_path))
		{
			$cache_path=temp_prel($path,$file);
		}
		return $cache_path;
	}

	function temp_prel($path,$file){

		$file_path=TEMP_PATH.$path."/".$file;
		$cache_path=CACHE_PATH.$path."/".$file.".tpl.php";

		if($path=="/"){
			$file_path=TEMP_PATH.$file;
			$cache_path=CACHE_PATH.$file.".tpl.php";
		}

		$content=@file_get_contents($file_path);
		if($content === false){
			$f=fopen($file_path,"r");
			$content=fread($f,filesize($file_path));
			fclose($f);
		}
		if(false===$content) exit("$file_path is not exists!");
		$content=template_parse($content);
		if(!is_dir(TEMP_PATH.$path)) @mkdir(TEMP_PATH.$path);
		if(!is_dir(CACHE_PATH.$path)) @mkdir(CACHE_PATH.$path);
		$strlen = @file_put_contents($cache_path, $content);
		if(false===$strlen){
			$f=fopen($cache_path,"w");
			flock($f,LOCK_EX);
			fwrite($f,$content);
			flock($f,LOCK_UN);
			fclose($f);
		}
		@chmod($cache_path, 0777);
		return $cache_path;
	}

	function template_parse($str)
{
	$str = preg_replace("/([\n\r]+)\t+/s","\\1",$str);
	$str = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}",$str);
	$str = preg_replace("/\{temp\s+(.+)\}/","<?php include temp(\\1); ?>",$str);
	$str = preg_replace("/\{include\s+(.+)\}/","<?php include \\1; ?>",$str);
	$str = preg_replace("/\{php\s+(.+)\}/","<?php \\1?>",$str);
	$str = preg_replace("/\{if\s+(.+?)\}/","<?php if(\\1) { ?>",$str);
	$str = preg_replace("/\{else\}/","<?php } else { ?>",$str);
	$str = preg_replace("/\{elseif\s+(.+?)\}/","<?php } elseif (\\1) { ?>",$str);
	$str = preg_replace("/\{\/if\}/","<?php } ?>",$str);
	$str = preg_replace("/\{for\s+(.+?)\}/","<?php for(\\1) { ?>",$str);
	$str = preg_replace("/\{\/for\}/","<?php } ?>",$str);
	$str = preg_replace("/\{foreach\s+(.+?)\}/","<?php foreach(\\1) { ?>",$str);
	$str = preg_replace("/\{\/foreach\}/","<?php } ?>",$str);
	$str = preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/","<?php echo \\1;?>",$str);
	$str = preg_replace("/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/","<?php echo \\1;?>",$str);
	$str = preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/","<?php echo \\1;?>",$str);
	$str = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/es", "addquote('<?php echo \\1;?>')",$str);
	$str = preg_replace("/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s", "<?php echo \\1;?>",$str);
	return $str;
}

function addquote($var)
{
	return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var));
}

*/

function new_addslashes($string)
{
	if(!is_array($string)) return addslashes($string);
	foreach($string as $key => $val) $string[$key] = new_addslashes($val);
	return $string;
}

function new_stripslashes($string)
{
	if(!is_array($string)) return stripslashes($string);
	foreach($string as $key => $val) $string[$key] = new_stripslashes($val);
	return $string;
}

function new_trim($string){
	if(!is_array($string)) return trim($string);
	foreach($string as $key => $val) $string[$key] = new_trim($val);
	return $string;
}

function new_htmlspecialchars($string){
	if(!is_array($string))	return htmlspecialchars($string);
	foreach($string as $k=>$v) $string[$k]=new_htmlspecialchars($v);
	return $string;
}

function escape($string)
{
	if(!is_array($string)){
		$string = str_replace('%20','',$string);
		$string = str_replace('%27','',$string);
		$string = str_replace('*','',$string);
		$string = str_replace('"','&quot;',$string);
		$string = str_replace("'",'',$string);
		$string = str_replace("\"",'',$string);
		//$string = str_replace('//','',$string);
		$string = str_replace(';','',$string);
		$string = str_replace('<','&lt;',$string);
		$string = str_replace('>','&gt;',$string);
		$string = str_replace('(','',$string);
		$string = str_replace(')','',$string);
		$string = str_replace("{",'',$string);
		$string = str_replace('}','',$string);
		return str_replace(array('\n', '\r'), array(chr(10), chr(13)), $string);
	}
	foreach($string as $key=>$val) $string[$key] = escape($val);
	return $string;
}

function filter_xss($string, $allowedtags = '', $disabledattributes = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavaible', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragdrop', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterupdate', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmoveout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload','iframe'))
{
	if(is_array($string))
	{
		foreach($string as $key => $val) $string[$key] = filter_xss($val, $allowedtags);
	}
	else
	{
		$string = preg_replace('/\s('.implode('|', $disabledattributes).').*?([\s\>])/', '\\2', preg_replace('/<(.*?)>/ie', "'<'.preg_replace(array('/javascript:[^\"\']*/i', '/(".implode('|', $disabledattributes).")[ \\t\\n]*=[ \\t\\n]*[\"\'][^\"\']*[\"\']/i', '/\s+/'), array('', '', ' '), stripslashes('\\1')) . '>'", strip_tags($string, $allowedtags)));
	}
	return $string;
}


function msg($message,$url=''){ 
	if(!$url)	$url=$_SERVER['HTTP_REFERER'];
	if(!$url)	exit("<script type=\"text/javascript\">alert('$message');window.history.back();</script>");
	if(is_int($url))	exit("<script type=\"text/javascript\">alert('$message');window.history.go(-$url);</script>");
	exit("<script type=\"text/javascript\">alert('$message');window.location.href='$url';</script>"); 
}
function redirect($href){
	@header("location : $href ");
	exit("<script type=\"text/javascript\">window.location.href='$href';</script>");
}

?>